﻿using System.Web.UI;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebControls;

namespace Trentacular.Office.Server.Web.UI.WebControls
{
    /// <summary>
    /// Facet Renderer that generates a icon, hyperlink, and a count.
    /// </summary>
    public class IconFacetRenderer : DefaultFacetRenderer
    {
        /// <summary>
        /// Gets or sets the image src.
        /// </summary>
        /// <value>The image src.</value>
        public string ImageSrc { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether to apply alpha filter.
        /// </summary>
        /// <value><c>true</c> if to apply alpha filter; otherwise, <c>false</c>.
        /// </value>
        public bool ApplyAlphaFilter { get; set; }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use
        /// composition-based implementation to create any child controls they contain
        /// in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!string.IsNullOrEmpty(ImageSrc))
            {
                Controls.Add(CreateImageControl());
                Controls.Add(new LiteralControl(" "));
            }

            base.CreateChildControls();
        }

        private Control CreateImageControl()
        {
            if (ApplyAlphaFilter)
                return new AlphaImage
                    {
                        Src = ImageSrc,
                        Alt = Facet.DisplayName
                    };

            return new HtmlImage
                {
                    Src = ImageSrc,
                    Alt = Facet.DisplayName
                };
        }
    }
}
